import axios from 'axios'
import Vue from 'vue'

const RNTY_BASE_URL = 'http://www.ruinatiyu.com/'
const API_ACCOUNT_RNTY_URL = 'http://api.account.ruinatiyu.com/'
// const API_ACCOUNT_RNTY_URL = 'http://192.168.3.13:9000/'
// const RNTY_SHOP_MALL_URL = 'http://192.168.3.149:8080/'
const RNTY_SHOP_MALL_URL = 'http://api.shop.ruinatiyu.com/'
const RNTY_SHOP_PC_URL = 'http://shop.ruinatiyu.com/'
const CLIENT_ID = 4
const CLIENT_SECRET = 'L60f0vTWC-7labXVGDoOj2HVns-YqkbO'
const _this = Vue.prototype

function authLogin() {
  request({
    method: 'post',
    baseURL: API_ACCOUNT_RNTY_URL,
    url: 'clients/authentication',
    data: {
      client_id: CLIENT_ID,
      client_secret: CLIENT_SECRET,
      redirect_url: window.location.href
    }
  }).then(({data: {auth_code, login_url}}) => {
    // console.log(auth_code)
    _this.$cookie.set('auth_code', auth_code,{ domain: '.ruinatiyu.com'})
    window.location.href = login_url;
    return false
  })
}
function request({method = 'get', baseURL, url, param = null, data = null, contentType = 'application/x-www-form-urlencoded'}) {

  axios.defaults.baseURL = baseURL
  axios.defaults.withCredentials = false

  axios.defaults.transformRequest = [function (data) {
    // Do whatever you want to transform the data
    let ret = ''
    for (let it in data) {
      ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
    }
    return ret
  }]

  axios.defaults.headers.post['Content-Type'] = contentType;
  return axios({
    method: method,
    url: url,
    params: param,
    data: data
  })
}
function tokenRequest({method = 'get', baseURL, url, param, data,contentType = 'application/x-www-form-urlencoded'}) {
  let token = _this.$cookie.get('rnty_access_token')
  if (!token) {
    authLogin()
    return
  }
  axios.defaults.baseURL = baseURL
  axios.defaults.transformRequest = [function (data) {
    // Do whatever you want to transform the data
    let ret = ''
    for (let it in data) {
      ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
    }
    return ret
  }]
  axios.defaults.withCredentials = false
  axios.defaults.headers.post['Content-Type'] = contentType;
  axios.defaults.headers.common['Authorization'] = `Bearer ${token}`
  return axios({
    method: method,
    url: url,
    params: param,
    data:data
  }).catch((error) => {
    if (error.response.status === 401) {
      authLogin()
    }
  })
}
function isRN (vm){
  console.log(vm.auth_code);
  
  if (vm.auth_code) {
    request({
      method: "POST",
      baseURL: API_ACCOUNT_RNTY_URL,
      url: "clients/auth",
      data: {
        auth_code: vm.auth_code
      }
    })
      .then(({ data }) => {
        // console.log(data)
        if(data.status){
          vm.redirect_url = data.data;
        }
        // else{
          // vm.$router.push({
          //   name: "error401",
          //   params: {
          //     error_data: "授权码已经过期"
          //   }
          // })
        // }
      })
  }
}
export default {
  RNTY_BASE_URL,
  API_ACCOUNT_RNTY_URL,
  RNTY_SHOP_MALL_URL,
  RNTY_SHOP_PC_URL,
  CLIENT_ID,
  CLIENT_SECRET,
  request,
  tokenRequest,
  authLogin,
  isRN
}